﻿using System;
using System.IO;
using System.Configuration;

using SBPweb.Forge.Contracts.Logging;

using SBPweb.Forge.Logger.Actors;

namespace SBPweb.Forge.Logger.Writers
{

    public class LogWriter : StreamWriter
    {

        #region Private fields

        private string format = "{0}\t[{1}]\t[{2}]\t{3}:\t{4}" + Environment.NewLine;

        #endregion

        #region Constructor logic

        public LogWriter(Stream stream)
            : base(stream)
        {
        }

        protected LogWriter(KeyValueConfigurationCollection settings)
            : base(new LogWriterStreamFactory().InstantiateStreamBasedOnSettings(settings))
        {
        }

        #endregion

        #region Property accessors

        public virtual string Format
        {
            get
            {
                return format;
            }
            set
            {
                format = value;
            }
        }

        #endregion

        #region Methods

        public void Write(LogItem item)
        {
            this.Write(format, DateTime.Now.ToString("yyyy.MM.dd. HH:mm:ss.fffff"), item.Severity, item.Category, item.Sender, item.Object);
        }

        public void Write(object sender, SeverityLevel severity, object obj, string category)
        {
            this.Write(new LogItem(sender, severity, obj, category));
        }

        #endregion

    }

}